package com.gzpi.hwgggh.service.impl;

import com.gzpi.hwgggh.entity.Dictionary;
import com.gzpi.hwgggh.mapper.DictionaryMapper;
import com.gzpi.hwgggh.service.DictionaryService;
import com.gzpi.hwgggh.sysenum.EnumErrorCode;
import com.gzpi.hwgggh.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date : 2021-01-04 18:46
 **/
@Service
@Transactional
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    @Cacheable(value="dictionary")
    public Result selectDicByKeyAndGroupKey(String KEY, String GROUPKEY) {

        try {
            Map<String,Object> map = new HashMap<>(); //条件map
            map.put("KEY",KEY); //标题
            map.put("GROUPKEY",GROUPKEY); //内容
            List<Dictionary> dictionaries = dictionaryMapper.selectDicByKeyAndGroupKey(map); //获取数据
            if (dictionaries.size()>0){
                return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),dictionaries);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString());
    }

    @Override
    @CacheEvict(value = "dictionary", allEntries=true)
    public Result removeCache() {
        return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("清除缓存成功"));
    }
}
